package ModelLayer;

import java.util.ArrayList;

public class OwnerContainer {
	private ArrayList<Owner> ownerContainer;
	
	//Singleton
	private static OwnerContainer instance;
	
	//Constructor
	private OwnerContainer(){
		ownerContainer = new ArrayList<Owner>();
	}
	
	/**
	 * Singleton
	 * @return OwnerContainer instance
	 * */
	public static OwnerContainer getInstance(){
		if(instance == null){
			instance = new OwnerContainer();
		}
		return instance;
	}
	
	/**
	 * Add owner to container
	 * @param Owner owner
	 * */
	public void addOwner(Owner owner){
		ownerContainer.add(owner);
	}
	
	/**
	 * Find owner by name and remove owner
	 * @param String name
	 * */
	public void removeOwner(String name){
		boolean search = false;
		int i = 0;
		Owner o = null;
		while(!search && i < ownerContainer.size()){
			o = ownerContainer.get(i);
			if(o.getOwnerName() == name){
				search = true;
			}else{
				i++;
			}
		}
		if(search){
			ownerContainer.remove(i);
		}
	}
	/**
	 * Find owner by name
	 * @param String name
	 * @return Owner or null
	 * */
	public Owner findOwner(String name){
		boolean search = false;
		int i = 0;
		Owner o = null;
		while(!search && i < ownerContainer.size()){
			o = ownerContainer.get(i);
			if(o.getOwnerName() == name){
				search = true;
			}else{
				i++;
			}
		}
		if(search){
			return ownerContainer.get(i);
		}else{
			return null;
		}
	}
	/**
	 * Find owner by phoneNumber
	 * @param String phoneNumber
	 * @return Owner or null
	 * */
	public Owner findOwnerByPhone(String phoneNumber){
		boolean search = false;
		int i = 0;
		Owner o = null;
		while(!search && i < ownerContainer.size()){
			o = ownerContainer.get(i);
			if(o.getPhoneNumber().contains(phoneNumber)){
				search = true;
			}else{
				i++;
			}
		}
		if(search){
			return ownerContainer.get(i);
		}else{
			return null;
		}
	}
	/**
	 * Remove owner by phone
	 * @param String phoneNumber
	 * */
	public void removeOwnerByPhone(String phone){
		Owner o = findOwnerByPhone(phone);
		if( o != null){
			removeOwner(o.getOwnerName());
		}
	}
}
